package com.naiterui.ehp.bs.cms.modules.agent.controller;

import cn.hutool.json.JSONUtil;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.security.utils.SecurityUtils;
import com.naiterui.ehp.bp.support.advice.IgnoreResponseEntityWrap;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.utils.FileUtil;
import com.naiterui.ehp.bs.cms.common.controller.AbstractController;
import com.naiterui.ehp.bs.cms.common.utils.JwtUser;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.agent.po.AgentAuditPO;
import com.naiterui.ehp.bs.cms.modules.agent.po.AgentEditPO;
import com.naiterui.ehp.bs.cms.modules.agent.po.AgentPagePO;
import com.naiterui.ehp.bs.cms.modules.agent.service.IAgentService;
import com.naiterui.ehp.bs.cms.modules.agent.util.ConstantUtil;
import com.naiterui.ehp.bs.cms.modules.agent.util.FilePathUtil;
import com.naiterui.ehp.bs.cms.modules.agent.vo.AgentAchievementListVO;
import com.naiterui.ehp.bs.cms.modules.agent.vo.AgentAchievementVO;
import com.naiterui.ehp.bs.cms.modules.agent.vo.AgentDetailVO;
import com.naiterui.ehp.bs.cms.modules.agent.vo.AgentPageItemVO;
import com.naiterui.ehp.bs.cms.modules.agent.vo.SearchItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author yiren
 * @since 2022/2/10
 */
@Slf4j
@Api(tags = {SwaggerTag.CMS + SwaggerTag.AGENT_MANGEN})
@AllArgsConstructor
@RestController
@RequestMapping("/agent")
@PreAuthorize("@p.check('agent')")
public class AgentController extends AbstractController {

	private final IAgentService agentService;

	@SysLog("经纪人列表")
	@GetMapping("/list")
	@ApiOperation("经纪人列表")
	// @PreAuthorize("@p.check('agent:list')")
	public PageVO<AgentPageItemVO> list(PageParamsVO pageParamsVO, AgentPagePO pagePO) {
		return agentService.queryAgentPage(pageParamsVO, pagePO);
	}

	@SysLog("查找经纪人")
	@GetMapping("/find")
	@ApiOperation("查找经纪人")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "searchTxt", value = "搜索内容", dataType = "string", paramType = "query", required = true),
	})
	// @PreAuthorize("@p.check('agent:find')")
	public List<SearchItemVO> find(String searchTxt) {
		return agentService.findData(searchTxt);
	}

	@SysLog("修改经纪人")
	@PostMapping("/save")
	@ApiOperation("修改经纪人")
	// @PreAuthorize("@p.check('agent:save')")
	public void save(@RequestBody AgentEditPO editPO) throws BusinessException {
		log.info("修改经纪人参数：{}", JSONUtil.toJsonStr(editPO));
		agentService.saveAgent(editPO);
	}

	@SysLog("经纪人详情")
	@GetMapping("/get")
	@ApiOperation("经纪人详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "经纪人ID", dataType = "long", paramType = "query", required = true),
	})
	// @PreAuthorize("@p.check('agent:get')")
	public AgentDetailVO getAgentDetail(Long id) throws BusinessException {

		return agentService.getDetail(id);
	}

	@SysLog("经纪人认证审核")
	@PostMapping("/audit")
	@ApiOperation("经纪人认证审核")
	// @PreAuthorize("@p.check('agent:audit')")
	public void audit(@RequestBody AgentAuditPO auditPO) throws BusinessException {
		log.info("经纪人认证审核参数：{}", JSONUtil.toJsonStr(auditPO));
		agentService.audit(auditPO);
	}

	@SysLog("经纪人批量导入")
	@PostMapping("/import")
	@ApiOperation("经纪人批量导入")
	// @PreAuthorize("@p.check('agent:import')")
	public void importData(MultipartFile file) throws BusinessException {
		JwtUser jwtUser = SecurityUtils.getCurrentUser();
		agentService.importData(file, jwtUser.getUsername());
	}

	@SysLog("经纪人业绩信息")
	@GetMapping("/achievement/info")
	@ApiOperation("经纪人业绩信息")
	// @PreAuthorize("@p.check('agent:achievement:info')")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "agentId", value = "经纪人Id", dataType = "long", paramType = "query", required = true),})
	public AgentAchievementVO achievement(@RequestParam Long agentId) {
		return agentService.achievement(agentId);
	}

	@SysLog("经纪人业绩信息列表")
	@GetMapping("/achievement/list")
	@ApiOperation("经纪人业绩信息列表")
	// @PreAuthorize("@p.check('agent:achievement:list')")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "agentId", value = "经纪人Id", dataType = "long", paramType = "query", required = true),
			@ApiImplicitParam(name = "reportType", value = "报告类型：1.年 2.月 3.日", dataType = "int", paramType = "query",
					required = true),
			@ApiImplicitParam(name = "reportDate", value = "年月日 yyyy-MM-dd格式", dataType = "string", paramType = "query",
					required = true)})
	public PageVO<AgentAchievementListVO> achievementList(PageParamsVO pageParamsVO, @RequestParam Long agentId,
														  @RequestParam Integer reportType, String reportDate) {
		return agentService.achievementList(pageParamsVO, agentId, reportType, reportDate);
	}

	@GetMapping("/achievement/export")
	@ApiOperation("经纪人业绩信息导出")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "agentIds", value = "经纪人id，多个经纪人id逗号分割", dataType = "string", paramType = "query",
					required = true),
			@ApiImplicitParam(name = "reportType", value = "报告类型：1.年 2.月 3.日", dataType = "int", paramType = "query",
					required = true),
			@ApiImplicitParam(name = "reportDates", value = "选择日期，多个日期以逗号分割", dataType = "string", paramType = "query")})
	@IgnoreResponseEntityWrap
	// @PreAuthorize("@p.check('// // @PreAuthorize(')")
	public void achievementExport(String agentIds, Integer reportType, String reportDates, HttpServletResponse response)
			throws IOException {
		log.info("经纪人业绩信息导出, agentIds: {}, reportType: {}, reportDates: {}", agentIds, reportType, reportDates);
		byte[] body = agentService.achievementExport(agentIds, reportType, reportDates);
		String fileName = System.getProperty("java.io.tmpdir") + File.separator + System.currentTimeMillis() + ".xls";
		FileUtil.downloadFile(this.request, response, FileUtil.writeBytes(body, fileName), true);
	}

	@SysLog("获取经纪人导入模板地址")
	@GetMapping("/template/get")
	@ApiOperation("获取经纪人导入模板地址")
	public String getTemplate() {
		return FilePathUtil.pathToUrl(ConstantUtil.IMPORT_AGENT_TEMPLATE);
	}

}
